const { Op } = require("sequelize");

const brannerModel = appRequire('models/branner');

// 导出对象，供其它模块调用
module.exports = {
    list,
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    let params = {}
    let orders = [['createdAt', 'DESC']]
    let course = await brannerModel.branner.findAndCountAll({
        where: params,
        order: orders,
        include: ['artPPT'],
    })
    course = Common.clone(course)
    for (let item of course.rows) {
        if(item.artPPT){
            if (item.image) {
                item.image = item.image + "?imageMogr2/format/WebP"
            } else {
                if (item.artPPT.images && item.artPPT.images.length == 0) {
                    item.image = item.artPPT.fileUrls[0].fileUrl + '?ci-process=doc-preview&dstType=png/imageMogr2/format/webp'
                }
            }
    
            item.objectId = item.artPPT.objectId
            item.typeDesc = item.artPPT.typeDesc
            item.title = item.artPPT.title
            item.artpptId = item.artPPT.objectId
            delete item.artPPT
        }
    }
    returnData.maxCount = course.count
    returnData.maxPage = Math.ceil(course.count / postData.pageSize)
    returnData.data = course.rows
    request.json(returnData)
}